package com.example.dev_demo.excel.demo;

import com.example.dev_demo.excel.exports.ExcelExportUtil;
import com.example.dev_demo.excel.exports.ExcelSheet;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class ExcelExportController {

    @RequestMapping("/exports")
    public void export(HttpServletResponse response) throws Exception {
        //导出Excel文件并通过HTTP响应返回
        // 1. 创建需要导出的Sheet数据（可以复用之前的示例代码）
        List<ExcelSheet> sheets = new ArrayList<>();
        sheets.add(ExcelExportDemo.createTopHeaderSheet());
//        sheets.add(ExcelExportDemo.createLeftHeaderSheet());
//        sheets.add(ExcelExportDemo.createLargeDataLeftHeaderSheet());

        // 2. 设置响应头信息
        String fileName = "Excel导出数据_" + System.currentTimeMillis() + ".xlsx";

        // 3. 通过工具类导出Excel到响应输出流
        ExcelExportUtil.exportExcel(sheets, response.getOutputStream(), response, fileName);
    }
}
